/*
 * 文件名：FieldEnum.java	 
 * 时     间：下午2:25:55
 * 作     者：Administrator       
 * 版     权： 2012-2022 湖南智软网络科技, 公司保留所有权利.
 * 联     系：http://www.073586.com/
 */
package com.zbc.lucene.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.util.Date;

import org.apache.lucene.document.Field;
import org.apache.lucene.document.LongField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;

import com.cz073586.tools.util.Log4jUtil;

/**
 * 类描述:这里是类描述
 * 
 * @ClassName: FieldEnum
 * @author Administrator
 * @date 2016年8月7日 下午2:25:55
 */
public enum FieldEnum
{
	STRING_FIELD("path")
	{
		@Override
		public StringField newFeild(Object value)
		{
			if (value instanceof String)
			{
				return new StringField(this.name(), (String) value,
						Field.Store.YES);
			}
			return null;
		}
	},
	MODIFY_TIME("modifyed")
	{
		private final Log4jUtil LOG = Log4jUtil.getLog4j(FieldEnum.class);
		
		@Override
		public LongField newFeild(Object value)
		{
			if (value != null)
			{
				String strDate = "";
				if (value instanceof Date)
				{
					strDate = String.valueOf(((Date) value).getTime());
				} else
				{
					strDate = value.toString();
				}
				try
				{
					return new LongField(this.name(), Long.valueOf(strDate),
							Field.Store.NO);
				} catch (NumberFormatException e)
				{
					LOG.error("{}", e);
				}
				
			}
			return null;
		}
	},
	CONTENTS("contents")
	{
		@Override
		public TextField newFeild(Object value)
		{
			if (value instanceof BufferedReader)
			{
				return new TextField(this.name(), (BufferedReader) value);
			} else if (value instanceof InputStream)
			{
				return new TextField(this.name(), new BufferedReader(
						new InputStreamReader((InputStream) value,
								StandardCharsets.UTF_8)));
			} else if (value instanceof String)
			{
				StringReader reader = new StringReader(value.toString());
				return new TextField(this.name(), reader);
			}
			return null;
		}
	};
	
	private String name;
	
	FieldEnum(String name)
	{
		this.name = name;
	}
	
	public abstract Field newFeild(Object value);
	
	public static String vauleOf(String name)
	{
		for (FieldEnum c : FieldEnum.values())
		{
			if (c.name.equals(name))
			{
				return c.name;
			}
		}
		return "";
	}
}
